﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


public enum EventEnum
{
    Type1 = 1,
    Type2 = 1 << 2,
    Any = Type1 | Type2
}

[CreateAssetMenu(fileName = "video_point.asset", menuName = "Custom/Create VideoAsset")]
public class VideoAsset : ScriptableObject
{
    [Tooltip("课程ID")] 
    public int ID;
    [Tooltip("课程名称")]
    public string Name;
    [Tooltip("时间点")]
    public float Time;
    [Tooltip("事件类型")]
    public EventEnum EventType;
}

public class VideoAssetEditor : EditorWindow
{
    [MenuItem("Test/VideoAsset Test")]
    public static void showWindow()
    {
        VideoAssetEditor window = GetWindow<VideoAssetEditor>("VideoAsset Editor");
        window.minSize = window.maxSize = new Vector2(300, 300);
        window.Init();
        window.Show();
    }


    private VideoAsset setting;
    private int ID;
    private string Name;
    private float Time;
    private EventEnum EventType;

    public void Init()
    {
        string settingPath = "Assets/Resources/VideoAssetTest.asset";
        setting = AssetDatabase.LoadAssetAtPath<VideoAsset>(settingPath);
        if (null == setting)
        {
            setting = ScriptableObject.CreateInstance<VideoAsset>();
            AssetDatabase.CreateAsset(setting, settingPath);
            AssetDatabase.SaveAssets();
        }

        ID = setting.ID;
        Name = setting.Name;
        Time = setting.Time;
        EventType = setting.EventType;
    }

    private void OnGUI()
    {
        EditorGUI.BeginChangeCheck();
        ID = EditorGUILayout.IntField("课程ID", ID);
        if (EditorGUI.EndChangeCheck())
        {
            setting.ID = ID;
        }

        EditorGUI.BeginChangeCheck();
        Name = EditorGUILayout.TextField("课程名称", Name);
        if (EditorGUI.EndChangeCheck())
        {
            setting.Name = Name;
        }


        EditorGUI.BeginChangeCheck();
        EventType = (EventEnum)EditorGUILayout.EnumFlagsField("事件类型", EventType);
        if (EditorGUI.EndChangeCheck())
        {
            setting.EventType = EventType;
        }


        if (GUILayout.Button("保存"))
        {
            EditorUtility.SetDirty(setting);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }

}
